﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CodeCamp.BL;
using CodeCamp.BL.Helpers;
using System.IO;
using CodeCamp.BL.UI;

namespace CodeCamp.Tests
{
	/// <summary>
	///This is a test class for ControllerHelperTest and is intended
	///to contain all ControllerHelperTest Unit Tests
	///</summary>
	[TestClass()]
	public class ControllerHelperTestController : Controller
	{

		[TestMethod]
		public void GetAllMenuItems_ReturnsActionsThatHaveAttribute()
		{
			Assembly assembly = Assembly.GetAssembly(GetType());
			Assert.IsNotNull(assembly);
			IList<MvcMenuItemAttribute> actual = MvcMenu.ListMenuItems(assembly);

			Assert.AreEqual(5, actual.Count);
		}

		[TestMethod]
		public void MenuHtmlExtension_RendesNestedLists()
		{
			Assembly assembly = Assembly.GetAssembly(GetType());
			Assert.IsNotNull(assembly);

			var menuItems = MvcMenu.ListMenuItems(assembly);

			var writer = new StringWriter();
			MvcMenu.RegisterMenuItems(menuItems);
			writer.ShowMenu();
			Assert.AreEqual(MENU_OUTPUT, writer.ToString());
			Console.WriteLine(writer.ToString());
		}

		#region Dummy actions for testing reflective menue loader

		[MvcMenuItem("Top Level 2", 11)]
		public ActionResult ShowMe1() { return null; }


		[MvcMenuItem("Top Level 1!", 8)]
		public ActionResult ShowMe2() { return null; }


		[MvcMenuItem("Hello There 2!", ParentLink = "/ControllerHelperTest/ShowMe1")]
		public ActionResult ShowMe3() { return null; }


		[MvcMenuItem("Hello There 3!", ParentLink = "/ControllerHelperTest/ShowMe1")]
		public ActionResult ShowMe4() { return null; }

		[MvcMenuItem("Hello There 4!", ParentLink = "/ControllerHelperTest/ShowMe3")]
		public ActionResult ShowMe5() { return null; }

		public ActionResult HideMe() { return null; }
		#endregion
		const string MENU_OUTPUT = @"<ul>
	<li><a href=""/ControllerHelperTest/ShowMe2"" alt=""Top Level 1!"">Top Level 1!</a></li><li><a href=""/ControllerHelperTest/ShowMe1"" alt=""Top Level 2"">Top Level 2</a><ul>
		<li><a href=""/ControllerHelperTest/ShowMe3"" alt=""Hello There 2!"">Hello There 2!</a><ul>
			<li><a href=""/ControllerHelperTest/ShowMe5"" alt=""Hello There 4!"">Hello There 4!</a></li>
		</ul></li><li><a href=""/ControllerHelperTest/ShowMe4"" alt=""Hello There 3!"">Hello There 3!</a></li>
	</ul></li>
</ul>";
	}
}